package myStruts;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import dao.PersonDao;
import vo.Person;

public class personAction extends deafaultAction {

	int index;

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String addform() throws Exception {
		return "addform";
	}

	public String add() throws Exception {

		this.LOG.info("add.......PersonAction....");
		this.LOG.info("vo.getUsername():" + vo.getUsername());
		PersonDao dao = new PersonDao();
		dao.insert(vo);
		java.util.ArrayList<Person> lo = shoppingcart();
		lo.add(vo);
		return "add";
	}

	public String list() throws Exception {
		this.LOG.info("list.......PersonAction....");
		// this.setRslist(this.shoppingcart());
		return "list";
	}

	java.util.ArrayList<Person> shoppingcart() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		java.util.ArrayList<Person> list = (java.util.ArrayList<Person>) session
				.getAttribute("list");
		if (list == null) {
			list = new java.util.ArrayList<Person>();
			session.setAttribute("list", list);
		}
		return list;
	}

	Person vo;

	public Person getVo() {
		return vo;
	}

	public void setVo(Person vo) {
		this.vo = vo;
	}

	// java.util.ArrayList<Person> rslist;

	public java.util.ArrayList<Person> getRslist() {
		return shoppingcart();
	}

	public String updateform() throws Exception {
		this.LOG.info("personAction.updateform");
		this.setVo(shoppingcart().get(index));
		return "updateform";
	}

	public String update() throws Exception {
		this.LOG.info("personAction.update");
		java.util.ArrayList<Person> list=shoppingcart();
		list.set(index, vo);
		return "update";
	}

	public String delete() throws Exception {
		this.LOG.info("personAction.delete");
		java.util.ArrayList<Person> list=shoppingcart();
		list.remove(index);
		return list();
	}

}
